package com.ydgg.ltar.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ydgg.ltar.api.ApiResult;
import com.ydgg.ltar.mapper.MemberMapper;
import com.ydgg.ltar.model.Member;
import com.ydgg.ltar.util.PageInfo;

/**
 * 会员管理
 * 作者： 张叶
 * 创建时间：2019年3月11日  下午3:05:38
 */
@Service
public class MemberService {

	@Autowired
	private MemberMapper memberMapper;
	

	/**
	 * 管理员列表，分页
	 * @author 张叶
	 * @version 创建时间：2016年7月9日 下午4:04:35
	 */
	public List<Map<String,Object>> selectByPage(PageInfo pageInfo, String where, String order) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		List<Map<String, Object>> m = memberMapper.selectByPage(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}
	
	
	@Transactional
	public ApiResult insert(String openId,String nickName,String imageUrl){
		ApiResult result = new ApiResult();
		//没有会员，新建会员
		Member m = memberMapper.findByOpenId(openId);
		int i = 0;
		if(m == null){//如果没有该会员，添加此会员
			m = new Member();
			m.setOpenId(openId);
			m.setNickName(nickName);
			m.setImageUrl(imageUrl);
			m.setCreatedAt(new Date());
			m.setStatus("on");
			i = memberMapper.insert(m);
		}else{
			m.setNickName(nickName);
			m.setImageUrl(imageUrl);
			i = memberMapper.updateByPrimaryKeySelective(m);
		}
		if(i > 0){
			result.msg.status = 0;
			result.msg.desc = "操作成功";
		}else{
			result.msg.status = 1;
			result.msg.desc = "操作失败";
		}
		return result;
	}
	
	public int update(Member m){
		int i = memberMapper.updateByPrimaryKey(m);
		return i;
	}
	
	
	public List<Member> selectByWhere(String where){
		List<Member> list = memberMapper.selectByWhere(where);
		return list;
	}
	
}
